package com.ruoyi.ihome.service.impl;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.biz.domain.IhCertification;
import com.ruoyi.biz.mapper.IhCertificationMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.ihome.domain.*;
import com.ruoyi.ihome.mapper.*;
import com.ruoyi.ihome.domain.TagAndHome;
import com.ruoyi.ihome.mapper.TagAndHomeMapper;
import com.ruoyi.ihome.service.ITagAndHomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ihome.service.IIhHomeService;

import javax.annotation.Resource;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 房间信息Service业务层处理
 *
 * @author NING
 * @date 2024-08-12
 */
@Service
public class IhHomeServiceImpl implements IIhHomeService {
    @Resource
    private IhHomeMapper ihHomeMapper;
    @Resource
    private RoomAndDistrictMapper roomAndDistrictMapper;
    @Resource
    private TagAndHomeMapper tagAndHomeMapper;
    @Resource
    private HomeAndFacilityMapper homeAndFacilityMapper;
    @Resource
    private ITagAndHomeService iTagAndHomeService;

    @Resource
    private IhCertificationMapper ihCertificationMapper;




    /**
     * 查询房间信息
     *
     * @param id 房间信息主键
     * @return 房间信息
     */
    @Override
    public IhHome selectIhHomeById(Long id) {
        TagAndHome tagAndHome = new TagAndHome(null,id, null);

        List<TagAndHome> tagAndHomes = iTagAndHomeService.selectTagAndHomeList(tagAndHome);

//        System.out.println("列表里的值为"+tagAndHomes);

//        System.out.println("列表里的值changdu为"+tagAndHomes.size());

        Long[] taglist=new Long[tagAndHomes.size()];

//        List<Long> list = new ArrayList<>(Arrays.asList(taglist));

//        list.remove(null);

//        taglist= list.toArray(new Long[0]);

        for (int j=0 ; j<tagAndHomes.size();j++){

            Long tagId = tagAndHomes.get(j).getTagId();

            taglist[j]=tagId;

            System.out.println(taglist[j]);

        }

//        List<Long> listS = new ArrayList<>(Arrays.asList(taglist));

//        listS.remove(null);

//        taglist= listS.toArray(new Long[0]);

        IhHome ihHome = ihHomeMapper.selectIhHomeById(id);

        ihHome.setTaglist(taglist);

//        return ihHomeMapper.updateIhHome(ihHome);



        return ihHome;
    }

    @Override
    public List<IhHome> selectIhHome(IhHome ihHome) {
        return ihHomeMapper.selectIhHome(ihHome);
    }

    /**
     * 查询房间信息列表
     *
     * @param ihHome 房间信息
     * @return 房间信息
     */
    @Override
    public List<IhHome> selectIhHomeList(IhHome ihHome) {
        return ihHomeMapper.selectIhHomeList(ihHome);
    }

    @Override
    public IhHome selectAllBylandorder(Long id) {
        return ihHomeMapper.selectAllBylandorder(id);
    }

    /**
     * 新增房间信息
     *
     * @param ihHome 房间信息
     * @return 结果
     */

    @Override
    public int insertIhHome(IhHome ihHome) {
        ihHome.setCreateTime(DateUtils.getNowDate());

        // 获取用户id 判断是否实名
        Long userId = ihHome.getUserId();

        // 设置查询条件
        IhCertification certification = new IhCertification();
        certification.setUserId(userId);

        List<IhCertification> ihCertifications = ihCertificationMapper.selectIhCertificationList(certification);
        if (ihCertifications == null)
            return 0;

        // 关联插入标签表
        int num = ihHomeMapper.insertIhHome(ihHome);
        Long houseId = ihHome.getId();
        Long[] ihHomeTag = ihHome.getIhHomeTags();
        for (Long tagId : ihHomeTag){
            num = num + tagAndHomeMapper.insertTagAndHome(new TagAndHome(null,houseId,tagId));
        }

        // 关联设施表
        Long[] ihHomeFacility = ihHome.getIhHomeFacility();
        for (Long facilityId: ihHomeFacility){
            num = num + homeAndFacilityMapper.insertHomeAndFacility(new HomeAndFacility(null,facilityId,houseId,null));
        }


        // 关联插入房间区域图片表
        List<List<String>> roomDistrictList = ihHome.getRoomDistrictList();
        System.out.println(roomDistrictList);
        if(roomDistrictList!=null){
            for(long i=0;i<roomDistrictList.size();i++){
                if(roomDistrictList.isEmpty() || roomDistrictList.get((int) i)==null)
                    continue;
                for (long j = 0L; j < roomDistrictList.get((int)i).size(); j++) {
                    roomAndDistrictMapper.insertRoomAndDistrict(new RoomAndDistrict(null,houseId, i,roomDistrictList.get((int)i).get((int)j)));
                }
            }
        }
        return num;
    }

    /**
     * 修改房间信息
     *
     * @param ihHome 房间信息
     * @return 结果
     */
    @Override
    public int updateIhHome(IhHome ihHome) {
        return ihHomeMapper.updateIhHome(ihHome);
    }

    /**
     * 批量删除房间信息
     *
     * @param ids 需要删除的房间信息主键
     * @return 结果
     */
    @Override
    public int deleteIhHomeByIds(Long[] ids) {
        for (Long id : ids) {
            /*房间区域中间表删除*/
            RoomAndDistrict roomAndDistrict1 = new RoomAndDistrict();
            roomAndDistrict1.setHouseId(id);
            List<RoomAndDistrict> roomAndDistricts = roomAndDistrictMapper.selectRoomAndDistrictList(roomAndDistrict1);
            for (RoomAndDistrict rad : roomAndDistricts) {
                roomAndDistrictMapper.deleteRoomAndDistrictById(rad.getId());
            }
            /*房间标签中间表删除*/
            TagAndHome tagAndHome = new TagAndHome();
            tagAndHome.setHouseId(id);
            List<TagAndHome> tagAndHomes = tagAndHomeMapper.selectTagAndHomeList(tagAndHome);
            for (TagAndHome th : tagAndHomes) {
                tagAndHomeMapper.deleteTagAndHomeById(th.getId());
            }
            /*房间设施中间表删除*/
            HomeAndFacility homeAndFacility = new HomeAndFacility();
            homeAndFacility.setHouseId(id);
            List<HomeAndFacility> homeAndFacilities = homeAndFacilityMapper.selectHomeAndFacilityList(homeAndFacility);
            for (HomeAndFacility hf : homeAndFacilities) {
                homeAndFacilityMapper.deleteHomeAndFacilityById(hf.getId());
            }
        }
        return ihHomeMapper.deleteIhHomeByIds(ids);
    }

    /**
     * 删除房间信息信息
     *
     * @param id 房间信息主键
     * @return 结果
     */
    @Override
    public int deleteIhHomeById(Long id) {
        return ihHomeMapper.deleteIhHomeById(id);
    }
}
